/***************************************** 
  Firm Survey Analysis 
*****************************************/

clear all
set more off
set segmentsize 3g
set scheme s1mono

* Load Data
use "$Data/Original/firm_manager_survey.dta", clear

* Note: The median survey duration is 115 minutes, while the mean is 831 minutes due to some respondents completing the survey a few days after opening it. Duplicate entries were resolved by retaining the last submission based on Full Name. A small number of early responses from HR managers, who served as points of contact in each factory, have been included in the dataset.


* Rename variables
rename Q1                             Full_Name
rename Q33                            Consent
rename Q36                            factory
rename Q2                             Age 	
rename Q3                             phone_number
rename Q4                             current_role
rename Q5                             tenure
rename Q6                             education_highest
rename Q7                             religion
rename Q8                             caste
rename Q22                            task_coord_com
rename Q23                            task_frictions
rename Q24                            task1_religion
rename Q39                            task2_religion
rename BB                             seg_religion_eff
rename BC                             seg_age_eff


* Label Vars
la var Full_Name "Full Name [redacted]"
la var Consent "Consent"
la var factory "Which of these factories do you work at? [redacted]"
la var Age "How old are you?"
la var phone_number "Phone number [redacted]"
la var current_role "Current role at the factory"
la var tenure "Tenure at the factory"
la var education_highest "Highest level of education"
la var religion "Religion"
la var caste "Caste"
la var task_coord_com "Which task (HD or LD) do you think requires more coordination and communication?"
la var task_frictions "Which task (HD or LD) do you think is likely to cause more arguments and frictions amongst workers?"
la var task1_religion "Which type type of team do you think would be more productive at this task?"
la var task2_religion  "Which type type of team do you think would be more productive at this task?"
la var seg_religion_eff "Willingness to segregate workers by religion"
la var seg_age_eff "Willingness to segregate workers by age"

* Encode categorical variables for summary stats
foreach var of varlist current_role-caste task_coord_com-seg_age_eff {
    quietly drop if `var' == "."
    encode `var', gen(`var'_encoded)
    drop `var'
    rename `var'_encoded `var'
}

/**********************************
 * Summary Stats Analysis 
 **********************************/

* Adjust variables for graphs on communication, coordination, and frictions
foreach var of varlist task_coord_com task_frictions {
	g `var'_plot = `var'
    replace `var'_plot = 0 if `var'_plot == 2
    replace `var'_plot = `var'_plot - 1 if `var'_plot >= 3
    replace `var'_plot = -`var'_plot
}

* Figure C.14: Perception of communication, coordination, and frictions across tasks
twoway (histogram task_coord_com_plot, percent color(blue%30) recast(dot) horizontal bin(4) ///
        xlabel(-0.375 "Cannot Say" -1.125 "Both equally" -1.875 "More in HD" -2.625 "More in LD")) ///
       (histogram task_frictions_plot, percent recast(dot) horizontal color(red%30) bin(4)), ///
    yoverhangs xoverhangs ///
	xscale(range(-2.8 -0.2)) /// Adjusted x-axis range for gaps
    legend(size(small) order(1 "Communication and Coordination" 2 "Arguments and Frictions")) ///
    xtitle("Responses") title("Firm Survey: HD and LD Task Characteristics", size(medium)) 	
graph export "$Output/Figures/TaskCC.pdf", as(pdf) replace



* Adjust variables for graphs on team performance based on tasks
foreach var of varlist task1_religion task2_religion {
	g `var'_plot = `var'
    replace `var'_plot = 0 if `var'_plot == 4
    replace `var'_plot = -1 if `var'_plot == 3
    replace `var'_plot = -`var'_plot
}

* Figure C.15: Perception of team performance based on tasks
twoway (histogram task1_religion_plot, percent color(blue%30) recast(dot) horizontal  bin(4) ///
		xlabel(-1.625 "Religiously Mixed" -0.875 "Religiously Homog" -0.125 "Both Equally" 0.625 "Cannot Say")) ///
       (histogram task2_religion_plot, percent color(red%30) recast(dot) horizontal lcolor(black) bin(4)), ///
    xoverhangs yoverhangs ///
	xscale(range(-1.8 0.8)) /// Adjusted x-axis range for gaps
    legend(order(1 "HD Task" 2 "LD Task")) ///
    xtitle("Responses") title("Firm Survey: Which team type would be more productive?", size(medium)) 
graph export "$Output/Figures/TaskHDLD_Mixed.pdf", as(pdf) replace


* Adjust variables for segregation by age or religion/caste
foreach var of varlist seg_religion_eff seg_age_eff {
    g `var'_plot = -`var'
}

* Figure C.16: Would you segregate workers based on religion or age?
twoway (histogram seg_religion_eff_plot, percent color(blue%30) recast(dot) horizontal bin(4) ///
        xlabel(-1.375 "Cannot Say" -2.125 "No, differences improve with time" -2.875 "No, segregation will raise tensions" -3.625 "Yes", labsize(vsmall))) ///
       (histogram seg_age_eff_plot, percent color(red%30) recast(dot) horizontal lcolor(black) bin(4)), ///
    yoverhangs xoverhangs ///
	xscale(range(-3.8 -1.2)) /// Adjusted x-axis range for gaps
	yscale(range(0 70)) /// Adjusted y-axis range for gaps
    legend(order(1 "Religion" 2 "Age")) ///
    xtitle("Responses", size(medium)) title("Firm Survey: Would you segregate workers on these dimensions?", size(medium)) 
	graph export "$Output/Figures/SegYN.pdf", as(pdf) replace
